package com.hp.bon.sgw.domain;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;

import com.hp.bon.sgw.bean.BlacklistRuleView;
import com.hp.bon.sgw.bean.CapabilityLifeBean;
import com.hp.bon.sgw.bean.CapabilityLimitCfgBean;
import com.hp.bon.sgw.bean.SensitivtyCfg;
import com.hp.bon.sgw.util.dcc.DccAvpHelper;

public class SgwViewDao {
	private SessionFactory sessionFactSgwView;
	
	@Autowired
	@Qualifier("sessionFactorySgwView")
	public void setSessionFactSgwView(SessionFactory pSessionFact) {
		this.sessionFactSgwView = pSessionFact;
	}
	
	/**
	 * @Desc 获取生命周期配置
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@Transactional(readOnly = true)
	public ConcurrentHashMap<String, CapabilityLifeBean> getCapabilityLifeCfg(){
		ConcurrentHashMap<String, CapabilityLifeBean> lifeMap = new ConcurrentHashMap<String, CapabilityLifeBean>();
		String sql = "select a.life_rule, d.ext_service_id, e.app_host_id, e.app_name,a.life_flag " +
					    "from cap_inst_life_t a, cap_instance_t b, cap_service_t c, service_info_t d, application_info_t e " +
					   "where a.cap_inst_id = b.cap_inst_id " +
					     "and b.app_id = e.app_id " +
					     "and b.cap_id = c.cap_id " +
					     "and c.service_id =  d.service_id " +
					     "and a.life_type_state = '00A'";
		Query query = this.sessionFactSgwView.getCurrentSession().createSQLQuery(sql);
		List list = query.list();
		if(list != null){
			for (int i = 0; i < list.size(); i++) {
				Object[] objArr = (Object[])list.get(i);
				CapabilityLifeBean lifeBean = new CapabilityLifeBean();
				lifeBean.setLifeRule(objArr[0].toString());
				lifeBean.setServiceContextId(objArr[1].toString());
				lifeBean.setHostId(objArr[2].toString());
				lifeBean.setAppName(objArr[3].toString());
				lifeBean.setLifeFlag(objArr[4].toString());
				lifeMap.put(lifeBean.getLifeKey(), lifeBean);
			}
			
		}
		
		return lifeMap;
	}
	
	/**
	 * @Desc 获取流控配置
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@Transactional(readOnly = true)
	public ConcurrentHashMap<String, CapabilityLimitCfgBean> getCapabilityLimitCfg(){
		ConcurrentHashMap<String, CapabilityLimitCfgBean> limitMap = new ConcurrentHashMap<String, CapabilityLimitCfgBean>();
		String sql = "select a.limit_cycle_unit, a.limit_cycle, a.limit_discount, a.limit_warning, a.limit_type, a.rec_time, " +
						    "d.ext_service_id, e.app_host_id, e.app_name, d.service_id " +
						 "from cap_inst_limit_t a, cap_instance_t b, cap_service_t c, service_info_t d, application_info_t e " +
						"where a.cap_inst_id = b.cap_inst_id " +
						"and b.app_id = e.app_id " +
						"and b.cap_id = c.cap_id " +
						"and c.service_id =  d.service_id " +
						"and a.record_state = '00A'";
		
		Query query = this.sessionFactSgwView.getCurrentSession().createSQLQuery(sql);
		List list = query.list();
		if(list != null){
			for (int i = 0; i < list.size(); i++) {
				Object[] objArr = (Object[])list.get(i);
				CapabilityLimitCfgBean bean = new CapabilityLimitCfgBean();
				bean.setLimitCycleUnit(objArr[0].toString());
				bean.setLimitCycle(Long.parseLong(objArr[1].toString()));
				bean.setLimitDiscount(Long.parseLong(objArr[2].toString()));
				bean.setLimitWarning(Long.parseLong(objArr[3].toString()));
				bean.setLimitType(Integer.parseInt(objArr[4].toString()));
//				bean.setRecTime(Long.parseLong(objArr[5].toString()));
				bean.setServiceContextId(objArr[6].toString());
				bean.setHostId(objArr[7].toString());
				bean.setAppName(objArr[8].toString());
				bean.calLimitCycleSecond();
				limitMap.put(bean.getLimitKey(), bean);
			}
		}
		
		return limitMap;
	}
	
	/**
	 * @Desc 获取黑名单配置
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@Transactional(readOnly = true)
	//@Transactional(value = "transactionManagerSgwView")
	public ConcurrentHashMap<String, BlacklistRuleView> getBlackRuleCfg(){
		ConcurrentHashMap<String, BlacklistRuleView> blackRuleMap = new ConcurrentHashMap<String, BlacklistRuleView>();
		String sql = "select a.id, a.key_xml_tag, a.black_rc, a.black_cycle, a.black_num, " +
						    "d.ext_service_id, e.app_host_id, e.app_name, d.service_id " +
						 "from blacklist_rule_t a, cap_instance_t b, cap_service_t c, service_info_t d, application_info_t e " +
						"where a.cap_inst_id = b.cap_inst_id " +
						"and b.app_id = e.app_id " +
						"and b.cap_id = c.cap_id " +
						"and c.service_id =  d.service_id " +
						"and a.record_state = '00A'";
		
		Query query = this.sessionFactSgwView.getCurrentSession().createSQLQuery(sql);
		List list = query.list();
		if(list != null){
			for (int i = 0; i < list.size(); i++) {
				Object[] objArr = (Object[])list.get(i);
				BlacklistRuleView bean = new BlacklistRuleView();
				bean.setId(Long.valueOf(objArr[0].toString()));
				bean.setKeyXmlTag(objArr[1].toString());
				bean.setRule(objArr[2].toString());
				bean.setBlackCycle(Long.valueOf(objArr[3].toString()));
				bean.setBlackNum(Long.valueOf(objArr[4].toString()));
				bean.setServiceContextId(objArr[5].toString());
				bean.setHostId(objArr[6].toString());
				bean.setAppName(objArr[7].toString());
				bean.setKeyAvpPath(DccAvpHelper.convertXPath2AvpPath(bean.getKeyXmlTag()));
				blackRuleMap.put(bean.getKey(), bean);
			}
		}
		
		return blackRuleMap;
	}
	
	/**
	 * @Desc 获取黑名单结果列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<BlackListResult> getBlackListResultList(){
		String hsql = "from BlackListResult where blackState='00A'";
		Query q = this.sessionFactSgwView.getCurrentSession().createQuery(hsql);
		List<BlackListResult> list = q.list();
		return list;
	}
	
	/**
	 * @Desc 保存黑名单结果
	 * @return
	 */
	@Transactional(value = "transactionManagerSgwView")
	public void saveBlackListResult(BlackListResult vo){
		this.sessionFactSgwView.getCurrentSession().save(vo);
	}
	
	/**
	 * @Desc 获取敏感信息配置列表
	 * @return
	 */
	@Transactional(readOnly = true)
	public ConcurrentHashMap<String, SensitivtyCfg> getSensitivtyCfg(){
		ConcurrentHashMap<String, SensitivtyCfg> cfgMap = new ConcurrentHashMap<String, SensitivtyCfg>();
		String hsql = "select a.sensitivty_level, a.sensitivty_field, b.service_id, b.ext_service_id, b.service_name, e.app_host_id, e.app_safe_level " +
						  "from sensitivty_info_t a, service_info_t b, cap_service_t c, cap_instance_t d, application_info_t e " +
						    "where a.service_id = b.service_id " +
						      "and b.service_id = c.service_id " +
						      "and c.cap_id = d.cap_id " +
						      "and d.app_id = e.app_id " +
						      "and a.record_state = '00A'";
		Query query = this.sessionFactSgwView.getCurrentSession().createSQLQuery(hsql);
		
		@SuppressWarnings("rawtypes")
		List list = query.list();
		if(list != null){
			for (int i = 0; i < list.size(); i++) {
				Object[] objArr = (Object[])list.get(i);
				SensitivtyCfg bean = new SensitivtyCfg();
				bean.setLevel(objArr[0].toString());
				bean.setField(objArr[1].toString());
				bean.setSerivceId(Long.valueOf(objArr[2].toString()));
				bean.setServiceContextId(objArr[3].toString());
				bean.setServiceName(objArr[4].toString());
				bean.setHostId(objArr[5].toString());
				bean.setSafeLevel(objArr[6].toString());
				cfgMap.put(bean.getCfgKey(), bean);
			}
		}
		return cfgMap;
	}
	
}
